<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );
class ReviewsController extends JController
{  
  function display()
  {
  	//Get document
    $document =& JFactory::getDocument();
    //get request of view name with default is 'all'  
    $viewName = JRequest::getVar('view', 'all');
    $viewType = $document->getType();
    //Get view with view type
    $view = &$this->getView($viewName, $viewType);
    //get model wiht the view Name from controller
    $model =& $this->getModel( $viewName, 'ReviewsModel' );
    if (!JError::isError( $model )) {
    	//set model in view
      $view->setModel( $model, true );
    }
    //set layout is default
    $view->setLayout('default');
    //Call display() of ReviewsViewAll class in view.html.php
    $view->display();
    //These code above in display can be replaced by one line code.
  	//parent::display();
  }
  //If user click submit in comment form in default_form.php,system call this funtion
  function comment()
  {
  	//Save comment
    global $option;
    $row =& JTable::getInstance('comment', 'Table');
    if (!$row->bind(JRequest::get('post'))) {
      echo "<script> alert('".$row->getError()."'); 
                window.history.go(-1); </script>\n";
      exit();
    }
    $row->comment_date = date( 'Y-m-d H:i:s' );
    $user =& JFactory::getUser();
    if($user->_table->id)
    {
      $row->user_id = $user->_table->id;
    }
    if (!$row->store()) {
      echo "<script> alert('".$row->getError()."'); 
                window.history.go(-1); </script>\n";
      exit();
    }
    $this->setRedirect('index.php?option=' . $option . '&id=' . $row->review_id . '&view=review', 
                                         'Comment Added.');
  }
}
?>